﻿/*-----------------------------------------------------------------------
 * <copyright file="MatterNameSelChildWin.xaml.cs" company="O-RID">
 *     Copyright (c)2010 O-RID Co.,Ltd.
 * </copyright>
 * <author>Zhang jun</author>
 * <summary>
 *          案件名 查询子窗体（copy from LineSelChildWin）
 *
 *     引用服务：WCFServers.WCFCommonQueryRef
 *     服务功能：案件名信息查询
 *   
 *     引用服务：WCFLog4Ref.WCFLog4Client
 *     服务功能：记录日志
 *
 *   子窗体返回查询用列表值： 
 *          ClientDataList.SelMatterNamesList（记录所选择的 案件名 信息, 没加逗号 --全局--）
 *   子窗体返回显示用值：
 *          retRadioButtonContent
 *
 *          作成日： 04/25/2011
 *          06/29/2011 修改 构造参数，用来确定显示值和传输值
 * </summary>
 *----------------------------------------------------------------------- 
 */



namespace Orid.Management.Statistics.OperatorUnitMangement.Common.Window
{

    using System;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.FunctionList;
    using Orid.Management.Statistics.OperatorUnitMangement.WCFServers.WCFCommonQueryRef;
    using childWindow = Orid.Management.Statistics.OperatorUnitMangement.Common.Window;
    using Wcf = Orid.Management.Statistics.OperatorUnitMangement.Common.Service;


    public partial class MatterNameSelChildWin : ChildWindow
    {

        /// <summary>
        /// 消息子窗口
        /// messbox
        /// </summary>
        private childWindow.MessageBox errorMessbox = null;

        /// <summary>
        /// 服务引用 (查询案件名信息用)
        /// wcfClient
        /// </summary>
        private WCFCommonQueryClient wcfClient = null;

        /// <summary>
        /// 日志服务定义
        /// wcfLogClient
        /// </summary>
        private WCFLog4Ref.WCFLog4Client wcfLogClient = null;

        /// <summary>
        /// 子窗体返回 显示 用值
        /// </summary>
        public StringBuilder retRadioButtonContent = null;


        /// <summary>
        /// 显示参数 Matter显示案件名或是ID
        /// 06/29/2011 EDIT
        /// </summary>
        private string VisMatterNameOrID = string.Empty;

        /// <summary>
        /// Tag参数 Matter显示案件名或是ID
        /// 06/29/2011 EDIT
        /// </summary>
        private string TagMatterNameOrID = string.Empty;

        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="strFirst">显示参数，显示Matter案件名或是ID。引用枚举MatterShape</param>
        /// <param name="strSecond">传输参数，显示Matter案件名或是ID。引用枚举MatterShape.</param>
        public MatterNameSelChildWin(string strVis, string strTag)
        {
            InitializeComponent();
            // 引用主题
            ((App)Application.Current).SetTheme((Grid)LayoutRoot);
            
            this.InitField();

            this.Loaded += new RoutedEventHandler(MatterNameSelChildWin_Loaded);

            //清空 案件名 信息记录表
            ClientDataList.SelMatterNamesList.Clear();

            //查询案件名信息 服务
            this.wcfClient.SelectMatterNameInforCompleted += new EventHandler<SelectMatterNameInforCompletedEventArgs>(wcfClient_SelectMatterNameInforCompleted);

            //选择所有案件名
            this.cbAllMatterNames.Click += new RoutedEventHandler(cbAllMatterNames_Click);

            this.UnitWrapping.Width = this.Width;

            this.VisMatterNameOrID = strVis;
            this.TagMatterNameOrID = strTag;
        }

        #region 变量初始化方法  InitField
        /// <summary>
        /// 变量初始化方法  InitField
        /// </summary>
        private void InitField()
        {
            this.errorMessbox = new childWindow.MessageBox();
            this.wcfClient = Wcf.Service.CommonQuery;
            this.wcfLogClient = Wcf.Service.Log4;
            this.retRadioButtonContent = new StringBuilder();
        }

        #endregion 变量初始化方法  InitField

        #region 页面载入
        /// <summary>
        /// 页面载入
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The instance containing the event data.</param>
        void MatterNameSelChildWin_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                IsCheckBoxSelect(false, true);
                
                // 调用 查询案件名信息 服务
                this.wcfClient.SelectMatterNameInforAsync();
            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + " 案件名查询子窗体 MatterNameSelChildWin_Loaded方法： " + ex.Message.ToString());
                return;
            }
        }
        
        #endregion 页面载入

        #region Wrapping控件里的CHECK控件的状态设置
        /// <summary>
        /// Wrapping控件里的CHECK控件的状态设置
        /// </summary>
        /// <param name="isChecked">是否全选择</param>
        /// <param name="isEnable">是否可用</param>
        private void IsCheckBoxSelect(bool isChecked, bool isEnable)
        {
            if (this.UnitWrapping == null ||
                    this.UnitWrapping.Children.Count < 0)
            {
                return;
            }

            for (int i = 0; i < this.UnitWrapping.Children.Count; i++)
            {
                CheckBox radioButton = this.UnitWrapping.Children[i] as CheckBox;
                radioButton.IsEnabled = isEnable;
                radioButton.IsChecked = isChecked;
            }
        }
        
        #endregion  Wrapping控件里的CHECK控件的状态设置

        #region 查询案件名信息 服务实现
        /// <summary>
        /// 查询案件名信息 服务实现
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The instance containing the event data.</param>
        void wcfClient_SelectMatterNameInforCompleted(object sender, SelectMatterNameInforCompletedEventArgs e)
        {
            try
            {
                if (e.Error == null)
                {
                    if (e.Result.Count > 0)
                    {
                        // ******RadioButton 改为 CheckBox**********//
                        // 动态生成CheckBox，并给CONTEXT赋值（案件名）
                        this.UnitWrapping.Children.Clear();

                        for (int i = 0; i < e.Result.Count; i++)
                        {
                            CheckBox cb = new CheckBox();

                            #region 判断显示值

                            // 显示值 判断
                            
                            if (this.VisMatterNameOrID.Equals(MatterShape.MatterID.ToString()))
                            {
                                //显示值 Content = VALUE
                                cb.Content = e.Result[i].LineID.ToString();
                            }

                            if (this.VisMatterNameOrID.Equals(MatterShape.MatterValue.ToString()))
                            {
                                //显示值 Content = VALUE
                                cb.Content = e.Result[i].LineValue.ToString();

                            }

                            if (this.VisMatterNameOrID.Equals(MatterShape.MatterName.ToString()))
                            {
                                //显示值 Content = VALUE
                                cb.Content = e.Result[i].LineName.ToString();

                            }

                            #endregion 判断显示值

                            #region 判断传输值

                            // 如果传输值 判断
                            if (this.TagMatterNameOrID.Equals(MatterShape.MatterID.ToString()))
                            {
                                //查询用值 Tag =NAME
                                cb.Tag = e.Result[i].LineID.ToString();
                            }

                            if (this.TagMatterNameOrID.Equals(MatterShape.MatterName.ToString()))
                            {
                                //查询用值 Tag =NAME
                                cb.Tag = e.Result[i].LineName.ToString();
                            }

                            if (this.TagMatterNameOrID.Equals(MatterShape.MatterValue.ToString()))
                            {
                                //查询用值 Tag =NAME
                                cb.Tag = e.Result[i].LineValue.ToString();
                            }

                            #endregion 判断传输值

                            cb.IsChecked = false;
                            cb.Margin = new Thickness(7, 2, 2, 0);
                            this.UnitWrapping.Children.Add(cb);
                        }
                        // *****************************************

                        //****选择所有案件名 控件*****
                        if (this.UnitWrapping != null && this.UnitWrapping.Children.Count > 0)
                        {
                            this.cbAllMatterNames.Visibility = Visibility.Visible;
                            //初始状态，所有案件名不选
                            this.cbAllMatterNames.IsChecked = false;
                        }
                        // *****************************************
                    }
                    else
                    {
                        throw new Exception("案件名列表查询值为空！");
                    }
                }
                else
                {
                    throw new Exception("取服务端案件名信息出错！" + e.Error.Message.ToString());
                }
            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + " 案件名 查询子窗体 wcfClient_SelectMatterNameInforCompleted方法： " + ex.Message.ToString());

                Common.FunctionList.FunctionClass.ShowMessageBox(
                   this.errorMessbox,
                   1,
                   "提示",
                   ex.Message.ToString(),
                   Colors.Gray,
                   true,
                   Visibility.Visible,
                   Visibility.Collapsed);
                return;
            }
        }

        #endregion 查询案件名信息 服务实现

        #region 选择所有的案件名
        /// <summary>
        /// 选择所有的案件名
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The instance containing the event data.</param>
        void cbAllMatterNames_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                CheckBox cb = sender as CheckBox;

                ClientDataList.SelMatterNamesList.Clear();

                if (retRadioButtonContent.Length > 0)
                {
                    retRadioButtonContent.Remove(0, retRadioButtonContent.Length);
                }

                //选择所有的案件名
                if (cb.IsChecked == true)
                {
                    if (this.UnitWrapping != null)
                    {
                        IsCheckBoxSelect(true, false);
                    }
                }
                else //不选所有案件名
                {
                    if (this.UnitWrapping != null)
                    {
                        IsCheckBoxSelect(false, true);
                    }
                }
            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + " 案件名选择子窗体 cbAllMatterNames_Click方法： " + ex.Message.ToString());
                return;
            }
        }

        #endregion 选择所有的案件名

        #region 确认按钮
        /// <summary>
        /// 确认按钮
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The instance containing the event data.</param>
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // ********RadioButton改为CheckBox******08/05/2010 edit**********
                // 判断哪个CheckBox是选中状态，并将值
                // 传给ClientDataList.SelMatterNamesList(查询用)
                //RetRadioButtonContent(显示值用)

                CheckBox radioButton;

                if (retRadioButtonContent.Length > 0)
                {
                    retRadioButtonContent.Remove(0, retRadioButtonContent.Length);
                }

                ClientDataList.SelMatterNamesList.Clear();

                for (int i = 0; i < this.UnitWrapping.Children.Count; i++)
                {
                    radioButton = this.UnitWrapping.Children[i] as CheckBox;

                    // 如果类型不一致则返回null
                    if (radioButton != null && radioButton.IsChecked == true)
                    {
                        // 不用加引号和逗号(查询用)
                        ClientDataList.SelMatterNamesList.Add(radioButton.Tag.ToString().Trim());

                        // 显示用
                        retRadioButtonContent.Append(radioButton.Content.ToString().Trim()+" ");
                    }
                }
                //如果全选，就显示 AllMatterNames
                if (this.cbAllMatterNames.IsChecked == true)
                {
                    if (retRadioButtonContent.Length > 0)
                    {
                        retRadioButtonContent.Remove(0, retRadioButtonContent.Length);
                    }
                    retRadioButtonContent.Append(this.cbAllMatterNames.Tag.ToString().Trim());
                }

                this.DialogResult = true;
            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + " 案件名 查询子窗体 OKButton_Click方法：" + ex.Message.ToString());
                return;
            }
        }

        #endregion 确认按钮

        #region 取消按钮
        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The instance containing the event data.</param>
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            //清空 案件名 信息记录表
            ClientDataList.SelMatterNamesList.Clear();
            if (retRadioButtonContent.Length > 0)
            {
                retRadioButtonContent.Remove(0, retRadioButtonContent.Length);
            }
            this.DialogResult = false;

        }

        #endregion 取消按钮
    }

    #region MATTER显示形式
    /// <summary>
    /// MATTER显示ID或是显示MATTERNAME的形式
    /// 06/27/0211 Add
    /// </summary>
    public enum MatterShape
    {
        /// <summary>
        /// 以MatterID的形式显示
        /// </summary>
        MatterID,

        /// <summary>
        /// 以MatterName的形式显示
        /// </summary>
        MatterName,
       
        MatterValue
    }

    #endregion MATTER显示形式

}

